/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.knet.sp.ncrm.modules.customer.potentialdistrecord.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.knet.sp.ncrm.common.config.Global;
import cn.knet.sp.ncrm.common.persistence.AjaxMessage;
import cn.knet.sp.ncrm.common.persistence.Page;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.common.web.BaseController;
import cn.knet.sp.ncrm.modules.customer.potentialdistrecord.entity.PotentialDistRecord;
import cn.knet.sp.ncrm.modules.customer.potentialdistrecord.service.PotentialDistRecordService;
import cn.knet.sp.ncrm.modules.customer.potentialdistrecord.vo.PotentialDistRecordVo;

/**
 * 分配记录Controller
 * @author 葛海洋
 * @version 2015-08-26
 */
@Controller
@RequestMapping(value = "${adminPath}/customer/potentialDistRecord")
public class PotentialDistRecordController extends BaseController {

	@Autowired
	private PotentialDistRecordService potentialDistRecordService;
	
	@ModelAttribute
	public PotentialDistRecord get(@RequestParam(required=false) String id) {
		PotentialDistRecord entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = potentialDistRecordService.get(id);
		}
		if (entity == null){
			entity = new PotentialDistRecord();
		}
		return entity;
	}
	
	@RequiresPermissions("customer:potentialDistRecord:view")
	@RequestMapping(value = {"list", ""})
	public String list(PotentialDistRecord potentialDistRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PotentialDistRecord> page = potentialDistRecordService.findPage(new Page<PotentialDistRecord>(request, response), potentialDistRecord); 
		model.addAttribute("page", page);
		return "modules/customer/potentialdistrecord/potentialDistRecordAjaxList";//ajax模式
		//return "modules/customer/potentialdistrecord/potentialDistRecordList";//form表单模式
	}

	@RequiresPermissions("customer:potentialDistRecord:view")
	@RequestMapping(value = "listAjaxData")
	@ResponseBody
	public Page listAjaxData(PotentialDistRecord potentialDistRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PotentialDistRecord> page = potentialDistRecordService.findPage(new Page<PotentialDistRecord>(request, response), potentialDistRecord);
		return page;
	}

	@RequiresPermissions("customer:potentialDistRecord:view")
	@RequestMapping(value = "form")
	public String form(PotentialDistRecord potentialDistRecord, Model model) {
	    potentialDistRecord.addHistoryFlag();
		model.addAttribute("potentialDistRecord", potentialDistRecord);
		return "modules/customer/potentialdistrecord/potentialDistRecordForm";
	}

	@RequiresPermissions("customer:potentialDistRecord:edit")
	@RequestMapping(value = "save")
	public String save(PotentialDistRecord potentialDistRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, potentialDistRecord)){
			return form(potentialDistRecord, model);
		}
		potentialDistRecordService.save(potentialDistRecord);
		addMessage(redirectAttributes, "保存分配记录成功");
		return "redirect:"+Global.getAdminPath()+"/customer/potentialDistRecord/?repage";
	}

	//ajax模式 删除
	@RequiresPermissions("customer:potentialDistRecord:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public Object delete(PotentialDistRecord potentialDistRecord, RedirectAttributes redirectAttributes) {
		AjaxMessage result = AjaxMessage.success();
		try {
			potentialDistRecordService.delete(potentialDistRecord);
			result.appendMessage("删除分配记录成功!");
		} catch (Exception e) {
			logger.warn("删除分配记录失败", e);
			result.makeError("删除分配记录失败!系统发生异常");
		}
		return result;
	}

	@RequestMapping("listDistData")
	@ResponseBody
	public Page<PotentialDistRecordVo> listDistData(PotentialDistRecord potentialDistRecord){
		Page<PotentialDistRecordVo> page = new Page();
		page.setList(potentialDistRecordService.findVoList(potentialDistRecord));
		return page;
	}

}